package com.colorlight.cloud.complete.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

@Getter
public enum SensorType implements EnumTrans<CompleteEntity.SensorType, SensorType> {
    /**
     * SENSOR_DEFAULT 默认无效
     * SENSOR_BROAD_TEMP 板载温度
     * SENSOR_BROAD_HUM 板载湿度
     * SENSOR_BROAD_VOLTAGE 板载电压
     * SENSOR_TEMP 外接温度
     * SENSOR_HUM 外接湿度
     * SENSOR_BRIGHTNESS 外接亮度
     * SENSOR_VOLTAGE 外接电压
     * SENSOR_NOISE 外接噪音
     * SENSOR_PM2_5 外接pm2.5
     * SENSOR_PM10 外接pm10
     * SENSOR_SMOKE 外接烟雾
     * SENSOR_ELECT 电磁传感器
     */
    SENSOR_DEFAULT(CompleteEntity.SensorType.SENSOR_DEFAULT),
    SENSOR_BROAD_TEMP(CompleteEntity.SensorType.SENSOR_BROAD_TEMP),
    SENSOR_BROAD_HUM(CompleteEntity.SensorType.SENSOR_BROAD_HUM),
    SENSOR_BROAD_VOLTAGE(CompleteEntity.SensorType.SENSOR_BROAD_VOLTAGE),
    SENSOR_TEMP(CompleteEntity.SensorType.SENSOR_TEMP),
    SENSOR_HUM(CompleteEntity.SensorType.SENSOR_HUM),
    SENSOR_BRIGHTNESS(CompleteEntity.SensorType.SENSOR_BRIGHTNESS),
    SENSOR_VOLTAGE(CompleteEntity.SensorType.SENSOR_VOLTAGE),
    SENSOR_NOISE(CompleteEntity.SensorType.SENSOR_NOISE),
    SENSOR_PM2_5(CompleteEntity.SensorType.SENSOR_PM2_5),
    SENSOR_PM10(CompleteEntity.SensorType.SENSOR_PM10),
    SENSOR_SMOKE(CompleteEntity.SensorType.SENSOR_SMOKE),
    SENSOR_ELECT(CompleteEntity.SensorType.SENSOR_ELECT),
    ;

    private final Integer id;
    private final String name;
    private final CompleteEntity.SensorType sensorType;

    SensorType(CompleteEntity.SensorType sensorType) {
        this.id = sensorType.getNumber();
        this.name = sensorType.name();
        this.sensorType = sensorType;
    }

    /**
     * @param sensorType
     * @return 对应的类型
     */
    public static SensorType getSensorType(CompleteEntity.SensorType sensorType) {
        Objects.requireNonNull(sensorType, "sensorType can not be null.");
        return Arrays.stream(SensorType.values())
                .filter(e -> e.name.equals(sensorType.name()))
                .findFirst()
                .orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
    }

    @Override
    public CompleteEntity.SensorType toProto() {
        return this.sensorType;
    }

    @Override
    public SensorType formProto(CompleteEntity.SensorType sensorType) {
        return getSensorType(sensorType);
    }
}

